#!/bin/sh
# 'done' was taken, no other option for the name of this script

TODO_FILE=${HOME}/.local/share/${USER}/.todo
TODO_DON_FILE=${HOME}/.local/share/${USER}/.todo-don

delete() {
    # move line to .todo-don file and prune empty lines
    sed "$1!d" "${TODO_FILE}" >> "${TODO_DON_FILE}"
    sed -i '/^$/d' "${TODO_DON_FILE}"
    # delete task from .todo
    sed -i "$1d" "${TODO_FILE}"
    # notify user
    echo "index $1 deleted"
}

display () {
    cat "${TODO_DON_FILE}"
    count=$(($(wc -l < "${TODO_DON_FILE}")))
    printf 'finished %s tasks\n' "${count}"
}

# check existence of todo file, create if not
[ ! -f "${TODO_DON_FILE}" ] && touch "${TODO_DON_FILE}"

# if there are no arguments, merely display
[ -z "$*" ] && display && exit 0

# if there are arguments, they must be a single numeric value
index=$(expr $1 + 1 - 1 2> /dev/null)
[ ! $? = 0 ] && echo "usage: don [index]
error: index must be numeric" && exit 1

delete "$index"
